package com.sxt.test;

import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.Test;

import com.sxt.entity.City;
import com.sxt.entity.User;
import com.sxt.factory.HibernateSessionFactory;

public class ManyToOne {
	/**
	 * 初步测试：查询
	 */
	@Test
	public void test1() {
		Session session = HibernateSessionFactory.getSession();
		User user = (User) session.get(User.class, 3);
		System.out.println(user);
		session.close();
	}

	/**
	 * 添加一个用户
	 */
	@Test
	public void test2() {
		Session session = HibernateSessionFactory.getSession();
		City city = (City) session.get(City.class, 2);
		Transaction tx = session.beginTransaction();
		User user = new User(null, "小明", 15, city);
		session.save(user);
		tx.commit();
		session.close();
	}
	
	/**
	 * 修改用户的城市
	 */
	@Test
	public void test3() {
		Session session = HibernateSessionFactory.getSession();
		City city = (City) session.get(City.class, 1);
		User user = (User) session.get(User.class, 3);
		user.setCity(city);
		Transaction tx = session.beginTransaction();
		session.update("User", user);
		tx.commit();
		session.close();
	}
}
